(function(){
	// from parent url
	window._requestContent = (function(){
		if(self.frameElement!= null && self.frameElement.tagName=="IFRAME"){
			return parent.window._requestContext;
		}else if(window.dialogArguments != null){
			return window.dialogArguments.parent.window._requestContext;
		}else{
			var strFullPath=window.document.location.href; 
			var strPath=window.document.location.pathname; 
			var pos=(strPath=="/") ? strFullPath.length-1 : strFullPath.indexOf(strPath); 
			var prePath=strFullPath.substring(0,pos); 
			var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1); 
			return(prePath+postPath);
		}
	})();

	window.reLoadGrid = function(pageNum , total){
		alert("not set function[reLoadGrid]!");
	};
	
	var RETURN_CODE = "return_code";	//
	var REQ_BODY	= "body";			//Map -> {data : [a1,a2,a3]}
										//total、pageSize、pageNum、datas 
	var RETURN_INFO = "return_info";	//
	
	function failure(json){
		alert("Error Code : " + json[RETURN_CODE] + 
			  "Error_Info : " + json[RETURN_INFO]);
	}
	
	var _util = window._util = {
		info : function(txt){
			if(console)
				console.log(txt);
		},
		//string to object
		encode : function(text){
			return eval("(" + text + ')');
		},
		//only key=value
		decode : function(obj){
			/*var text = "{";
			for(var _n in obj)
				text += "\""+_n+"\":" + "\""+obj[_n]+"\"," ;
			text = text.substr(0 , text.length - 1);
			text += "}";*/
			var text = "&";
			for(var _n in obj)
				text += _n + "=" + obj[_n] + "&";
			return text;
		},
		ajax : function(opt){
			
			if(opt.url != ""){
				opt.url = window._requestContent + opt.url ;
				opt.data = opt.data || {};
				opt.dataType = "json";
				opt.type = "POST";
				opt.timeout = 1000*15 ;
				opt.success = function(json){
					if(!json)
						return ;
					_util.info(json);
					//解析结构
					_util.info(1);
					if(json[RETURN_CODE]){
						_util.info(2);
						if(json[RETURN_CODE] == "0"){
							_util.info(3);
							opt.success(json[REQ_BODY]);
						}else{
							failure(json);
							opt.failure(json);
						}
					}else{
						alert("Parse data error!");
						_util.info(json);
					}
				};
				opt.error = function(request , error , exception){
					alert(error);
					if(opt.error)
						opt.error(request);
					_util.info(request);
				};
				
				$.ajax(opt);
			}else{
				_util.info("请求Url不可以为空!");
			}
		},
		synRequest : function(opt){
			var conn = function(){
				var http;
				var activeX = ['MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
			
				try {
					http = new XMLHttpRequest();
				} catch (e) {
					for (var i = 0; i < activeX.length; ++i) {
						try {
							http = new ActiveXObject(activeX[i]);
							break;
						} catch (e) {
							var a = e ;
							a["a"] = 1;
						}
					}
				} finally {
					return http;
				}
				return http ;
			}();
			
			conn.open("POST", window._requestContent + opt.url, false);
			conn.setRequestHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=UTF-8");
			//conn.setRequestHeader("Content-Type","application/json;charset=UTF-8");
			//opt.data
			if(typeof(opt.data) == "object")
				opt.data = _util.decode(opt.data||"") ;
			//conn.send(opt.data);
			conn.send(opt.data);
			if(conn.status == 200){
				_util.info("------------------Syn-----------------" );
				_util.info(conn);
				
				//eval("var result = eval(conn.responseText)");
				var result = eval("(" + conn.responseText + ')');
				if(result){
					if(result[RETURN_CODE] == "0"){
						//return result[REQ_BODY];
						return result ;
					}else{
						failure(result);
						return null ;
					}
				}else{
					alert("response Error! \n" );
					return null ;
				}
			}else{
				_util.info("sysRequest Error : " + conn.status );
			}
		}
			
	};
})();